/*
 * Copyright (C) 2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

/*
 * Prototypes
 */
#if PROTO
static void
vo_rational_fp(vo_t *u, f_t *v, eint32_t m);

static void
vo_rational_dp(vo_t *u, d_t *v, eint32_t m);

static void
vo_rational_vo(vo_t *u, vo_t *v, eint32_t m);
#endif

/*
 * Implementation
 */
#if CODE
void
evector_rational(evector_t *v, etype_t t)
{
    eint32_t	 l;
    evector_t	*r;
    v_enter();

    switch (t) {
	case t_int8:	case t_uint8:
	case t_int16:	case t_uint16:
	case t_int32:	case t_uint32:
	case t_int64:	case t_uint64:
	    /* integers are already rationals */
	    thread_self->obj = v;
	    return;
	case t_float32:	case t_float64:
	case t_void:
	    break;
	default:
	    eraise_exception(except_not_a_number);
    }

    v_check(1);
    l = v->length;
    r = create(v, t_void);
    switch (t) {
	case t_float32:
	    vo_rational_fp(r->v.obj, v->v.obj, l);
	    break;
	case t_float64:
	    vo_rational_dp(r->v.obj, v->v.obj, l);
	    break;
	default:
	    vo_rational_vo(r->v.obj, v->v.obj, l);
	    break;
    }
    v_leave();
}

static void
vo_rational_fp(vo_t *u, f_t *v, eint32_t m)
{
    for (; m; m--, u++, v++) {
	switch (eq_rational_d(thr_qr, *v)) {
	    case -1:
		set_z(thr_zr);
		break;
	    case 0:
		set_q(thr_qr);
		break;
	    default:
		set_i(empz_get_i(thr_zr));
		break;
	}
    }
}

static void
vo_rational_dp(vo_t *u, d_t *v, eint32_t m)
{
    for (; m; m--, u++, v++) {
	switch (eq_rational_d(thr_qr, *v)) {
	    case -1:
		set_z(thr_zr);
		break;
	    case 0:
		set_q(thr_qr);
		break;
	    default:
		set_i(empz_get_i(thr_zr));
		break;
	}
    }
}

static void
vo_rational_vo(vo_t *u, vo_t *v, eint32_t m)
{
    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		set_i(0);
		break;
	    case t_int:
		set_i(get_i(v));
		break;
	    case t_float:
		switch (eq_rational_d(thr_qr, get_d(v))) {
		    case -1:
			set_z(thr_zr);
			break;
		    case 0:
			set_q(thr_qr);
			break;
		    default:
			set_i(empz_get_i(thr_zr));
			break;
		}
		break;
	    case t_mpz:
		set_z(get_z(v));
		break;
	    case t_mpq:
		set_q(get_q(v));
		break;
	    case t_mpr:
		switch (eq_rational_r(thr_qr, get_r(v))) {
		    case -1:
			set_z(thr_zr);
			break;
		    case 0:
			set_q(thr_qr);
			break;
		    default:
			set_i(empz_get_i(thr_zr));
			break;
		}
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
}
#endif
